package com.sxy;

import com.sxy.mapper.StuMapper;
import com.sxy.mapper.UserMapper;
import com.sxy.pojo.Stu;
import com.sxy.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
public class MybatisDemo02 {
    public static void main(String[] args) throws Exception{
        //1.加载mybatis的核心配置文件，获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2.获取对应的SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.执行sql语句
//        List<User> list = sqlSession.selectList("test.selectAll");
        //获取对应的UserMapper接口的代理对象,并执行sql
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        List<Stu> list = mapper.selectAll();
        //4.处理结果

        for (Stu o :list) {
            System.out.println(o);
        }

        //5.释放资源 只需释放SqlSession即可
        sqlSession.close();
    }
}
